from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from django.contrib.auth.models import Permission, ContentType

from apps.meiduo_admin.serializers.permissions import PermissionSerializer, ContentTypeSerializer
from apps.meiduo_admin.utils import UserPageNum


class PermissionView(ModelViewSet):
    '''
        用户权限表的增删改查
    '''
    # 指定查询集
    queryset = Permission.objects.all()
    # 指定序列化器
    serializer_class = PermissionSerializer
    # 指定分页器
    pagination_class = UserPageNum
    # 指定权限
    permission_classes = [IsAdminUser]

    # 获取权限类型
    def content_type(self, request):
        # 1. 获取所有权限类型
        content_type = ContentType.objects.all()
        # 2. 返回权限数据
        s = ContentTypeSerializer(content_type, many=True)
        return Response(s.data)